
var logic ={

	status:null,
	set:false,
	
	initialize:function(s){
		
		$('saveButton').set('disabled',!this.set);
		
		var select=$('menupopupSelect');
		select.empty();
		
		var causes=window.parent.PChimeraInterface.pProjectManagment.getProject().getCauses();
		
		var activeCause=window.parent.PChimeraInterface.pProjectManagment.getProject().getCauseAtIndex(window.parent.PChimeraInterface.pProjectManagment.selectedCauseIndex);
		
		for(var i=0;i<causes.length;i++){
			
			if(causes[i].getType()=="function"){
				
				var option=new Element("menuitem",{'label':causes[i].getName()});
				
				if(activeCause.getName()==causes[i].getName()){
					option.setAttribute('disabled',"true");
					option.set('label',causes[i].getName()+'[current Function]');
				}
				else if(i>window.parent.PChimeraInterface.pProjectManagment.selectedCauseIndex){
					option.setAttribute('disabled',"true");
					option.set('label',causes[i].getName()+'[function defined after active cause]');
				}
				//else option.set('text',causes[i].getName());
				
				select.adopt(option);
			}
		}
		
		if(s!=null){
			
			$('functionSelect').selectedIndex=s.functionIndex;
		}
		
	},
		
	getCode: function(){
		var projmana=window.top.PBrowserOverlay.getInterface().PChimeraInterface.pProjectManagment;
		var proj=window.top.PBrowserOverlay.getInterface().PChimeraInterface.pProjectManagment.getProject();
		var selectCause=proj.getCauseAtIndex(projmana.selectedCauseIndex);
		var selectEffect=selectCause.getEffectAtIndex(projmana.selectedEffectIndex);
		
		var preset=window.top.UChimera.getActivePreset()
		var select=$('functionSelect');
		if(select.selectedIndex!=-1){
			var funcName=select.selectedItem.label;
		
			var string='F'+funcName.text+'();';
			
			var state={functionName:funcName, functionIndex:select.selectedIndex, dur:0};
			
			//soluzione poco sicura...
			var functionCalled=window.parent.PChimeraInterface.pProjectManagment.getProject().getCauseAtIndex(select.selectedIndex);
			var effectsCalled=functionCalled.getEffects();
			if(effectsCalled!=null){
				for(var i=0;i<effectsCalled.length;i++){
					state.dur+=effectsCalled[i].getDuration();
				}
			}
			
			preset.setStatus(state);
			preset.setCode(string);
			selectEffect.injectable=true;
			alert("Preset Setted");
			this.set=!this.set;
			$('saveButton').set('disabled',!this.set);
		}
	},
	
			
	modifiedInterface:function(){
		//mantengo questa parte di codice per test successivi
		/*if(!this.timer){
				
			this.timer=window.setTimeout(function(){
			logic.modifiedInterfaceTest();},100);
		}
		else{
			clearTimeout(this.timer);
			this.timer=window.setTimeout(function(){
				logic.modifiedInterfaceTest();},100);
		}*/
		this.set=!this.set;
		$('saveButton').set('disabled',!this.set);
	}
};
